window.g_Buff = new class CBuffManager {
    constructor() {
        this.data = {};
        this.poisonTime = 20;
        this.fireTime = 3;
    }
    addPoisonBuff(uid) {
        if (!this.data[uid]) {
            this.data[uid] = {
                num: 0,
            }
        }
        if (!this.data[uid]['poison']) {
            this.data[uid]['poison'] = {
                time: this.poisonTime,
                interval: 0,
                fps: 1,
            }
        }
        if (this.data[uid].fire && this.data[uid].poison) {
            this.data[uid].num = 2;
        }
        this.data[uid].poison.time = this.poisonTime;//刷新中毒时间
        let monster = g_Monster.getMonsterByUid(uid);
        if (!monster) {
            return;
        }
        if (!monster.pBuff) {
            let preBuff = cc.instantiate(g_Res.getRes("buff", "poisonBuff"));
            monster.addChild(preBuff);
            monster.pBuff = preBuff;
        }
    }
    addFireBuff(uid) {
        if (!this.data[uid]) {
            this.data[uid] = {
                num: 0,
            }
        }
        if (!this.data[uid]['fire']) {
            this.data[uid]['fire'] = {
                time: this.fireTime,
                interval: 0,
                fps: 1,
            }
        }
        if (this.data[uid].poison && this.data[uid].fire) {
            this.data[uid].num = 2;
        }
        this.data[uid].fire.time = this.fireTime;//刷新时间
        //给怪物添加一个灼烧动画
        let tMonster = g_Monster.getMonsterByUid(uid);
        if (!tMonster) {
            //怪物不存在
            return;
        }
        if (!tMonster.fBuff) {
            let preFire = cc.instantiate(g_Res.getRes("buff", "fireBuff"));
            tMonster.addChild(preFire);//添加上了，怪物死亡自动消失
            tMonster.fBuff = preFire;//添加灼烧特效，方便后续删除
        }
    }
    calPoisonHurt(monster) {
        let pBuff = game.role.script.getPoison();
        let attack = game.role.script.getAttack();
        let hurt = pBuff * 0.2 * attack + pBuff * 5;
        g_Monster.onHurt(monster, hurt, 2);
    }
    calFireonHurt(monster) {
        let fBuff = game.role.script.getFire();
        let attack = game.role.script.getAttack();
        let hurt = fBuff * 0.2 * attack + fBuff * 5;
        g_Monster.onHurt(monster, hurt, 4);
    }
    checkPoison(dt) {
        //遍历表中，找到表中中毒的怪物调用伤害计算函数
        //不能直接把data数据删除
        //理想是一个怪物所有buff都没有的时候再删除id
        let tData = {};
        for (let id in this.data) {
            if (!this.data[id].poison) {
                continue;
            }
            let info = this.data[id].poison;
            info.time -= dt;
            let monster = g_Monster.getMonsterByUid(id);
            if (!monster) {
                continue;
            }
            info.interval += dt;
            if (info.interval >= info.fps) {
                info.interval -= info.fps;
                this.calPoisonHurt(monster);
            }
            if (info.time <= 0) {
                //毒buff时间过了
                monster.pBuff.destroy();
                monster.pBuff = null;
                //只删除id中毒buff
                delete this.data[id]['poison'];
                this.data[id].num--;
                continue;
            }
            // if (!tData[id]) {
            //     tData[id] = {};
            // }
            // tData[id]['poison'] = info;
        }
        // this.data = tData;
    }
    checkFire(dt) {
        let tData = {};
        for (let id in this.data) {
            if (!this.data[id].fire) {
                //如果怪物没有灼烧就直接跳过
                continue;
            }
            let info = this.data[id].fire;
            info.time -= dt;
            let tMonster = g_Monster.getMonsterByUid(id);
            if (!tMonster) {
                return;
            }
            info.interval += dt;
            if (info.interval >= info.fps) {
                info.interval -= info.fps;
                this.calFireonHurt(tMonster);
            }
            if (info.time <= 0) {
                this.data[id].num--;
                delete this.data[id]['fire'];
                tMonster.fBuff.destroy();
                tMonster.fBuff = null;
                continue;
                //从data里面删除这个怪兽
            }
            // if (!tData[id]) {
            //     tData[id] = {};
            // }
            // tdata[id]["fire"] = info;
        }
    }
    checkBuff() {
        let tData = {};
        for (let id in this.data) {
            if (this.data[id].num <= 0) {
                continue;
            }
            tData[id] = this.data[id];
        }
        this.data = tData;
    }
    update(dt) {
        this.checkPoison(dt);
        this.checkFire(dt);
        this.checkBuff();
    }

} 